package com.zr.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.jasper.tagplugins.jstl.core.When;

import com.zr.model.Teacher;
import com.zr.util.JDBCUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class TeacherDao {

	/** 
	 * 查询所有老师的名字
	 * @return 返回Json 格式的数据
	 */
	Map<String, String> teachers =  new HashMap<String, String>();
	public JSONArray findAllTeacher(){
		JSONArray joa =new JSONArray();
		String sql = "select tid,tname from teacher";
		Connection con = JDBCUtil.getJDBConnection();
		PreparedStatement pst;
		try {
			pst = con.prepareStatement(sql);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				String teacher = rs.getString("tname");
				String tid = String.valueOf(rs.getInt("tid"));
				teachers.put("id", tid);
				teachers.put("text", teacher);
				joa.add(teachers);

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.closedConnection(con);
		}
		
		
		
		return joa;
		
	}
	/** 
	 * 查询老师ID
	 * @param teacherName 传入老师名字
	 * @return 返回老师ID
	 */
	public int findTeacherId(String teacherName){
		int tid = 0;
		String sql = "select tid from teacher where tname=? or tid = ?";
		Connection con = JDBCUtil.getJDBConnection();
		PreparedStatement pst;
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, teacherName);
			pst.setString(2, teacherName);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				
				tid = rs.getInt("tid");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.closedConnection(con);
		}
		
		
		
		return tid;
		
	}
}
